'''
Created on 2010-12-24

@author: zarra
'''
from model.expr_b import Doctor,expr_b
import cherrypy,os
import lxml.html as HTML 
from ext.direct.cherrypy_utils import ActionManager
import conf


def quick_test_doctor(id=None):
    doctor_id = id if id != None else 'test'
    try:
        d=cherrypy.session.get('doctor',None)
    except:
        d=None
        
    if d == None:
       
        d=expr_b().query(Doctor).filter(Doctor.login_name==doctor_id).first()
       
        try:
            cherrypy.session['doctor']=d
        except:
            pass
    return d


def modify_base_url(func):
    def _f(*a,**kw):
        result = func(*a,**kw)
        dom=HTML.document_fromstring(result)
       
        tags={'script':'src',
              'link':'href'
              }
        for k,v in tags.items():
            elements = dom.xpath("//"+k)  
            for e in elements:
                src = e.attrib.get(v,None)
                if src != None and conf.SITE_ROOT != '/':
                    e.attrib[v]=conf.SITE_ROOT+src
        return HTML.tostring(dom, pretty_print=True,include_meta_content_type=True)
    return _f

def javascript_url_page():
    site_root = conf.SITE_ROOT if conf.SITE_ROOT != '/' else ''
    def do_page():
        page=file(os.path.join(conf.TEMPLATE_PATH,'utils.js')).read()
        try:
            cmtime = conf.cmtime
        except:
            cmtime=''
        try:
            version = conf.version
        except:
            version = ''
        return page%{'site_root':site_root,\
                     'cmtime':cmtime,\
                     'version':version}
    
    return do_page
class ExprActionManager(ActionManager):
    def __init__(self,base):
        super(ExprActionManager,self).__init__(base)
        self.direct_base_url   = base if conf.SITE_ROOT == '/' else conf.SITE_ROOT+base
        